<?php
class Upload{
	//文件保存目录路径
	public $save_path;
	//文件保存目录URL
	public $save_url;
	//定义允许上传的文件扩展名
	public $ext_arr;
	//最大文件大小
	public $max_size;
	public $file_name;
	public $tmp_name;
	public $file_size;
	//水印图片
	public $waterImage;
	//是否生成水印
	public $iswater;
	//所上传的文件夹
	public $path;
	public $isthumb;

	public function __construct(){
		set_time_limit(600);//设置脚本执行时间
		$this->save_path = BASE_ROOT.'images/'.BASE_SITE.'product/';
		$this->save_url = 'images/'.BASE_SITE.'product/';
		$this->ext_arr = array('gif', 'jpg', 'jpeg', 'png');
		$this->max_size = 10485760;
		$this->waterImage = BASE_ROOT.'View/'.BASE_SITE.'admin/images/water.png';
		$this->iswater = true;
		$this->isthumb = true;
		$this->path = 'product';
		//更改目录权限
		@mkdir($this->save_path, 0777);
	}
	public function setIsWater($is){
		$this->iswater=$is;
	}

	public function setPath($path){
		$this->save_path = BASE_ROOT.'images/'.BASE_SITE.$path.'/';
		$this->save_url = 'images/'.BASE_SITE.$path.'/';
		$this->path = $path;
	}

	public function setIsThumb($is) {
		$this->isthumb = $is;
	}

	public function saveFile(){
		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$this->file_name = $_FILES['imgFile']['name'];
			//服务器上临时文件名
			$this->tmp_name = $_FILES['imgFile']['tmp_name'];
			//文件大小
			$this->file_size = $_FILES['imgFile']['size'];
			//检查文件名
			if (!$this->file_name) {
				$this->alert("请选择文件。");
			}
			//检查目录
			if (@is_dir($this->save_path) === false) {
				$this->alert("上传目录不存在。".$this->save_path);
			}
			//检查目录写权限
			if (@is_writable($this->save_path) === false) {
				$this->alert("上传目录没有写权限。");
			}
			//检查是否已上传
			if (@is_uploaded_file($this->tmp_name) === false) {
				$this->alert("临时文件可能不是上传文件。");
			}
			//检查文件大小
			if ($this->file_size > $this->max_size) {
				$this->alert("上传文件大小超过限制。");
			}
			//获得文件扩展名
			$temp_arr = explode(".", $this->file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $this->ext_arr) === false) {
				$this->alert("上传文件扩展名是不允许的扩展名。");
			}
			//检查重名
			if (is_file($this->save_path . $this->file_name)){
				$this->file_name= mktime().$this->file_name;
			}
			//移动文件
			$file_path = $this->save_path . $this->file_name;
			if (move_uploaded_file($this->tmp_name, $file_path) === false) {
				$this->alert("上传文件失败。");
			}
			$this->imageWaterMark($file_path,$this->waterImage);
			if($this->path=='news'){
				echo '<html>';
				echo '<head>';
				echo '<title>Insert Image</title>';
				echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
				echo '</head>';
				echo '<body>';
				echo '<script type="text/javascript">parent.KE.plugin["image"].insert("' . $_POST['id'] . '", "../../'.$this->save_url.$this->file_name.'","' . $_POST['imgTitle'] . '","' . $_POST['imgWidth'] . '","' . $_POST['imgHeight'] . '","' . $_POST['imgBorder'] . '");</script>';
				echo '</body>';
				echo '</html>';
			}else{
				echo '<html>';
				echo '<head>';
				echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
				echo '<title>上传成功</title>';
				echo '<style type="text/css">*{margin:0;padding0}html,body{margin:0;padding:0}</style>';
				echo '</head>';
				echo '<body>';
				echo '<script language="JavaScript">
							parent.document.getElementById("pro_img").value = "'.$this->file_name.'";
					  </script>';
				echo '<table border="0" style="font-size:12px" height="50"><tr><td>';
				echo '<span style="color:red;">',$this->file_name,'图片上传成功! </span><a href="',site_url(),'upload/repeat/path/',$this->path,'/file/',urlencode($this->file_name),'">删除重传</a>';
				echo '</td><td>';
				echo '<img src="../../'.$this->save_url.'thumb_'.$this->file_name.'" height="50"/>';
				echo '<td></tr></table>';
				echo '</body>';
				echo '</html>';
			}
		}
	}
	public function alert($s){
		if($this->path=='news'){
			echo '<html>';
			echo '<head>';
			echo '<title>error</title>';
			echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
			echo '</head>';
			echo '<body>';
			echo '<script type="text/javascript">alert("'.$s.'");history.back();</script>';
			echo '</body>';
			echo '</html>';
			exit;
		}else{
			echo '<span style="color:red;font-size:12px">',$s,'</span>';
			echo '<a href="',site_url(),'upload/',$this->path,'"><span style="font-size:12px">重新上传</span></a>';
			exit;
		}
	}
	public function imageWaterMark($groundImage,$waterImage="",$waterPos=5){
		 
		$formatMsg = "暂不支持该文件格式，请用图片处理软件将图片转换为GIF、JPG、PNG格式。"; 
		$compressSize = 700;//压缩后的宽度
		$thumbsize = 200; //缩略图宽度

		//读取水印文件 
		if(!empty($waterImage) && file_exists($waterImage)) {
			$water_info = getimagesize($waterImage);
			$water_w   = $water_info[0];//取得水印图片的宽 
			$water_h   = $water_info[1];//取得水印图片的高 

			switch($water_info[2])//取得水印图片的格式 
			{
				case 1:$water_im = imagecreatefromgif($waterImage);break;
				case 2:$water_im = imagecreatefromjpeg($waterImage);break;
				case 3:$water_im = imagecreatefrompng($waterImage);break;
				default:die($formatMsg);
			}
		}

		//读取背景图片 
		if(!empty($groundImage) && file_exists($groundImage)) {
			$ground_info = getimagesize($groundImage);
			$ground_w   = $ground_info[0];//取得背景图片的宽 
			$ground_h   = $ground_info[1];//取得背景图片的高 

			switch($ground_info[2])//取得背景图片的格式 
			{
				case 1:$ground_im = imagecreatefromgif($groundImage);break;
				case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
				case 3:$ground_im = imagecreatefrompng($groundImage);break;
				default:die($formatMsg);
			}
			//压缩图片
			if($ground_w>$compressSize){
				$oground_w = $ground_w;
				$oground_h = $ground_h;
				$ground_h = floor($ground_h *( 1- (($ground_w-$compressSize)/$ground_w)));
				$ground_w = $compressSize;
				$ground_im = $this->ImageResize($groundImage,$ground_im,$ground_w,$ground_h,$oground_w,$oground_h);
			}
		}else{
			$this->alert("需要加水印的图片不存在！"); 
		}

		if( ($ground_w<$water_w) || ($ground_h<$water_h) ) {
			$this->alert("需要加水印的图片的长度或宽度比水印图片还小，无法生成水印！"); 
			return;
		}
		switch($waterPos)
		{
			case 0://随机 
				$posX = rand(0,($ground_w - $water_w));
				$posY = rand(0,($ground_h - $water_h));
				break;
			case 1://1为顶端居左 
				$posX = 0;
				$posY = 0;
				break;
			case 2://2为顶端居中 
				$posX = ($ground_w - $water_w) / 2;
				$posY = 0;
				break;
			case 3://3为顶端居右 
				$posX = $ground_w - $water_w;
				$posY = 0;
				break;
			case 4://4为中部居左 
				$posX = 0;
				$posY = ($ground_h - $water_h) / 2;
				break;
			case 5://5为中部居中 
				$posX = ($ground_w - $water_w) / 2;
				$posY = ($ground_h - $water_h) / 2;
				break;
			case 6://6为中部居右 
				$posX = $ground_w - $water_w;
				$posY = ($ground_h - $water_h) / 2;
				break;
			case 7://7为底端居左 
				$posX = 0;
				$posY = $ground_h - $water_h;
				break;
			case 8://8为底端居中 
				$posX = ($ground_w - $water_w) / 2;
				$posY = $ground_h - $water_h;
				break;
			case 9://9为底端居右 
				$posX = $ground_w - $water_w;
				$posY = $ground_h - $water_h;
				break;
			default://随机 
				$posX = rand(0,($ground_w - $water_w));
				$posY = rand(0,($ground_h - $water_h));
				break;
		}
		if($this->iswater){
	  //设定图像的混色模式 
			imagealphablending($ground_im, true);

			imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件 
			imagejpeg($ground_im,$groundImage);
		}

		if($this->isthumb){
			//生成缩略图
			$thumb_h = floor($ground_h *( 1- (($ground_w-$thumbsize)/$ground_w)));
			$thumb_w = $thumbsize;
			$thumbImage = $this->save_path .'thumb_'. $this->file_name;
			$this->ImageResize($thumbImage,$ground_im,$thumb_w,$thumb_h,$ground_w,$ground_h);
		}

		//释放内存 
		if(isset($water_info)) unset($water_info);
		if(isset($water_im)) imagedestroy($water_im);
		if(isset($ground_info)) unset($ground_info);
		if(isset($ground_im)) imagedestroy($ground_im);
	}
	public function ImageResize($groundImage,$pImage, $t_width, $t_height, $s_width, $s_height) {
		if(function_exists("imagecopyresampled")) {
			$iCanvas = imagecreatetruecolor($t_width, $t_height);
			imagecopyresampled($iCanvas, $pImage, 0, 0, 0, 0, $t_width, $t_height, $s_width, $s_height);
		}
		else {
			$iCanvas = imagecreate($t_width, $s_width);
			imagecopyresized($iCanvas, $pImage, 0, 0, 0, 0, $t_width, $t_height, $s_width, $s_height);
		}
		imagejpeg($iCanvas,$groundImage);
		return $iCanvas;
	}
}
?>